package servlet.admin;

import com.google.gson.Gson;
import entity.User;
import service.Impl.UserServiceImpl;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/admin/UserFindByName")
public class UserFindByNameServlet extends HttpServlet {
    private UserService userServiceDao = new UserServiceImpl();
    private Gson gson = new Gson(); // 创建 Gson 实例

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        List<User> users = userServiceDao.findUserByName(name);

        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        String json = gson.toJson(users);
        resp.getWriter().write(json);
    }
}
